import sys
from BasicLibraries import *
import _C_process_data as process_data
import _D_analysis as analysis
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning) 
warnings.filterwarnings("ignore", category=FutureWarning) 

ONEDRIVE = ''

#%%
for up_to in [2021]:
    print('Estimating processed data up to:', up_to)
    optimization_method = 'GA'
    #==== Choose your population
    population_type_ = 'LandscapePopulation_0724_full.txt'
    feature_type_ = 'opt_'+optimization_method+'_feature_importance_0724.pckl'
    process_type_ = 'opt_'+optimization_method+'_process_data_0724.pckl'
    stat_type_ = 'opt_'+optimization_method+'_deviation_stat_0724.pckl'
    tmp_outputs_ = 'tmp_outputs/opt_'+optimization_method+'_forest_'
    permutation_test_type = ['impurity', 'permutation'][1]
    #====
    
    
    ACTIONS = [  'association',
                 'adoption of standards and rules',
                 'assessment and measurement',
                 'organizational structuring',
                 'modification of procedures',
                 'asset modification',
                 'training',
                 'r&d investments',
                 'new products',
         ]
    sdgs = ['water & energy', 'cons. & prod.', 'biodiversity']
    
    
    for FW in list(np.linspace(0,1,5)):
        #=== name the output files
        population_type = str(FW).replace('.', '_')+'_'+population_type_
        feature_type = str(FW).replace('.', '_')+'_'+feature_type_
        process_type = str(FW).replace('.', '_')+'_'+process_type_
        stat_type = str(FW).replace('.', '_')+'_'+stat_type_
        tmp_outputs = tmp_outputs_+str(FW).replace('.', '_')+'_'
    
        #== Process the output
        normal_fit = False
        out0, res, prf, optima, observed, feature_importance, feature_irrelevance = process_data.process_data_to_return_object(permutation_test_type, population_type, 
                                                                                                                               feature_type, tmp_outputs, final_year = up_to)
        average_deviation_b, sdg_dev_b, act_dev_b, mec_dev, act, chl, sdg_devDIST, act_devDIST, mec_devDIST =  analysis.get_analysis_binary( optima, observed)
       
        #== Get the statistics to create the figures
        ## Save information on actions, sdgs, type of fit (oob or CV), permutation test
        pickle.dump([ACTIONS, sdgs, normal_fit,  permutation_test_type, out0, res, prf, optima,  observed, feature_importance, feature_irrelevance], open(process_type.replace('.pckl', '_'+str(up_to)+'.pckl'), 'wb'))
        pickle.dump([average_deviation_b, sdg_dev_b, act_dev_b, mec_dev, act, chl, sdg_devDIST, act_devDIST, mec_devDIST], open(stat_type.replace('.pckl', '_'+str(up_to)+'.pckl'), 'wb'))
        
        
    
    
    
    
    